package com.lanou.lenanapp.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.adapter.JokeReviewHotAdapter;
import com.lanou.lenanapp.adapter.JokeReviewNewAdapter;
import com.lanou.lenanapp.beans.JokeReview;
import com.lanou.lenanapp.beans.JokeReviewHot;
import com.lanou.lenanapp.util.Utils;
import com.lanou.lenanapp.view.HotListView;
import com.lanou.lenanapp.view.NewListView;
import com.lanou.lenanapp.volley.VolleySingleton;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论
 */
public class ReviewActivity extends Activity implements View.OnClickListener {

    private ListView hotlistView;
    private ListView newListView;
    private JokeReviewNewAdapter newAdapter;
    private JokeReviewHotAdapter hotAdapter;

    VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private String id;
    private TextView hot_title_text, new_title_text;
    private ImageView back;
    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_review);

        getData();
        init();
        initData();
    }

    private void getData() {
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
    }


    private void init() {
        hotlistView = (HotListView) findViewById(R.id.hotListView);
        hotAdapter = new JokeReviewHotAdapter();

        newListView = (NewListView) findViewById(R.id.newListView);
        newAdapter = new JokeReviewNewAdapter();

        //加载表头布局
        View view = getLayoutInflater().inflate(R.layout.hot_review_title, null);
        hot_title_text = (TextView) view.findViewById(R.id.hot_title_text);
        View view1 = getLayoutInflater().inflate(R.layout.new_review_title, null);
        new_title_text = (TextView) view1.findViewById(R.id.new_title_text);

        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(this);


    }

    private void initData() {
        dialog = Utils.createloadingDialog(this, "玩命加载中....");
        dialog.show();
        String url = "http://120.55.151.67/weibofun/comments_list.php?apiver=10700&fid="+ id +
                "&uid=-1&category=weibo_pics&page=0&page_size=15&max_cid=-1&get_post=0&platform=iphone&appver=1.9&buildver=1.9.4&udid=0FD678F7-07AA-4EBF-BB39-EB79EAB40753&sysver=7.0.4";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                if (response != null && response.has("hot")){

                    try {
                        JSONArray js = response.getJSONArray("hot");
                        List<JokeReviewHot> hots = new ArrayList<JokeReviewHot>();
                        Gson gson = new Gson();
                        for(int j = 0; j < js.length(); j++){
                            String st = js.getString(j);
                            JokeReviewHot jokeReviewHot = gson.fromJson(st, JokeReviewHot.class);
                            hots.add(jokeReviewHot);
                        }
                        hotAdapter.addData(hots);
                        //判断是否有最热评论
                        if (hots.size() > 0){
                            //如果有  则添加最热评论的表头
                            hotlistView.addHeaderView(hot_title_text);
                        }
                        //绑定适配器
                        hotlistView.setAdapter(hotAdapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                if (response != null && response.has("items")){
                    dialog.dismiss();
                    try {
                        JSONArray jr = response.getJSONArray("items");
                        List<JokeReview> items = new ArrayList<JokeReview>();
                        Gson gson = new Gson();
                        for (int i = 0; i < jr.length(); i ++ ){
                            String str = jr.getString(i);
                            JokeReview jokeReview = gson.fromJson(str, JokeReview.class);
                            items.add(jokeReview);
                        }

                        newAdapter.addData(items);
                        //判断是否有评论
                        if (items.size() > 0){
                            //如果有评论 则添加最新评论的表头
                            newListView.addHeaderView(new_title_text);
                        }
                        //绑定适配器
                        newListView.setAdapter(newAdapter);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        volleySingleton.addRequest(request);
    }






    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();//关闭当前页面
            break;
        }
    }


}
